<?php

namespace app\admin\model\wxapp;

use app\admin\model\store\Renew;
use app\common\model\Wxapp as WxappModel;
use app\admin\model\store\User as StoreUser;
use app\admin\model\agent\Agent as AgentModel;

/**
 * 板块模型
 */
class Wxapp extends WxappModel{
    /**
     * 获取板块信息
     */
    public static function details($wxappId = null){
        return static::get($wxappId);
    }
    /**
     * 获取板块列表
     * @param boolean $is_recycle
     */
    public function getList($serach){
        if(!empty($serach['search'])){
            $where['name']=['like','%'.$serach['search'].'%'];
        }else{
            $where='1=1';
        }
        return $this->where($where)->order(['createTime' => 'desc'])->paginate(15, false, ['query' => request()->request()]);
    }

    /**
     * 新增板块信息
     */
    public function add($data){
        $data['createTime']=time();
        $data['updateTime']=0;
        $data['status']=1;
        if (empty($data['name'])) {
            $this->error = '板块名称不能为空';
            return false;
        }
        return $this->allowField(true)->save($data);
    }
    /**
     * 编辑板块信息
     */
    public function edit($data){
        $data['updateTime']=time();
        $data['status']=1;
        if(empty($data['name'])) {
            $this->error = '板块名称不能为空';
            return false;
        }
        return $this->allowField(true)->save($data);
    }

    /**
     * 移入移出回收站
     */
    public function recycle($is_recycle = true){
        return $this->save(['status' => (int)$is_recycle]);
    }

    /**
     * 软删除
     */
    public function setDelete(){
        return $this->transaction(function () {
            // 设置当前商城为已删除
            return $this->allowField(true)->save(['status'=>2]);
        });
    }
    /**
     * 恢复板块
     */
    public function recover(){
        return $this->transaction(function () {
            // 设置当前商城为已删除
            return $this->allowField(true)->save(['status'=>1]);
        });
    }
}
